#!/bin/bash/python
# _*_ coding: utf-8 _*_
# file： auto_demo1
# date-time: 2023/8/30-14:44
# auth:hard work
# description
from stc_utils.tcl_util.stc_base import set_stc_base_cfg
tcl, tx_port, rx_port, chassis_ip, stc_file = set_stc_base_cfg()


def normal_eth2_stream():
    logpath = r"E:\share\stc_auto\stc_log\log7.log"
    capture_path = r"E:\share\stc_auto\stc_capture"
    tcl.setvar("logpath", logpath)
    tcl.eval("stc::config automationoptions -logTo $logpath -logLevel WARN")
    tcl.setvar("result_path", capture_path)
    # chassis_ip = '10.11.0.248'
    tcl.setvar('chassisAddress', chassis_ip)
    # tx_port = '2/1'
    tcl.setvar('slotPort1', tx_port)
    # rx_port = '2/2'
    tcl.setvar('slotPort2', rx_port)
    # E:\Program Files\Spirent Communications\Spirent TestCenter 5.42\Spirent TestCenter Application
    #  source {F:\Program Files\Spirent Communications\Spirent TestCenter 5.41\Spirent TestCenter Application\SpirentTestCenter.tcl}
    #
    tcl.evalfile(stc_file)
    tcl.eval("""
             #source {E:\Program Files\Spirent Communications\Spirent TestCenter 5.42\Spirent TestCenter Application\SpirentTestCenter.tcl}
             #stc::config automationoptions -logTo $logpath -logLevel WARN
             set chassisAddress $chassisAddress
             set slotPort1 $slotPort1 
             set slotPort2 $slotPort2
             set Project [stc::create project]
             set TxPort [stc::create port -under $Project]
             set RxPort [stc::create port -under $Project]
             set portReturn [stc::config $TxPort -location "//$chassisAddress/$slotPort1"]
             set portReturn [stc::config $RxPort -location "//$chassisAddress/$slotPort2"]
             set captureRx [stc::get $RxPort -children-capture]
             set ether [stc::create "EthernetFiber" -under $TxPort] 
             set streamBlock [stc::create "StreamBlock" -under $TxPort -frameConfig "" -InsertSig FALSE  -name "StreamBlock_1"] 
             puts 1
             set StrEthII [stc::create ethernet:EthernetII -under $streamBlock \
                                          -name eht_2 \
                                          -srcMac 00:00:0A:01:00:01 \
                                          -dstMac 00:00:0A:01:01:01 ] 

           #stc::config $streamBlock -LoadUnit MEGABITS_PER_SECOND -Load [expr 1*$RateFactor]
           set generator [ stc::get $TxPort -children-Generator]
            puts 2
           set generatorCfg [stc::get $generator -children-GeneratorConfig ]
           stc::config $generatorCfg -SchedulingMode "RATE_BASED" \
                    -DurationMode "BURSTS" \
                    -BurstSize 1 \
                    -Duration 1 
           set analyzer [stc::get $RxPort -children-Analyzer ]
           stc::apply
           #stc::perform CaptureStartAll -captureProxyIds $capture
           stc::perform CaptureStart -captureProxyId $captureRx
           puts 3
           stc::perform analyzerStart -analyzerList $analyzer
           stc::sleep 1
           stc::perform generatorStart -generatorList $generator
           stc::sleep 1
           stc::perform GeneratorStop -generatorList $generator
           stc::perform CaptureStopAll -captureProxyIds $captureRx
           puts 4
           stc::perform AnalyzerStop -analyzerList $analyzer
           stc::perform SaveToTcc -FileName "STC2.tcc"

            #set analyzernum [lindex $analyzer $i]
            #set result [stc::get $analyzer -children-AnalyzerPortResults]
            #set resultcount [stc::get $result -TotalFrameCount]
            #set captureresult [lindex $captureRx $i]
            stc::perform CaptureDataSave -captureProxyId $captureRx \
                                        -FileName "AftFiltering-step2_port.pcap" \
                                        -FileNamePath $result_path 
           """)


if __name__ == '__main__':
    normal_eth2_stream()
